package meconsea.life.cms.web.tags;

import java.io.Writer;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.IteratorStatus;

import com.opensymphony.xwork2.util.ValueStack;

public class ArtList extends Component {
	private static final Logger log = Logger.getLogger(ArtList.class);
	private static final long serialVersionUID = -1827978132393591801L;
	protected Iterator iterator;
	protected IteratorStatus status;
	protected Object oldStatus;
	protected IteratorStatus.StatusState statusState;
	protected String statusAttr;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected PageContext pageContext;
	public ArtList(ValueStack stack, HttpServletRequest req,
			HttpServletResponse res, PageContext pc) {
		super(stack);
		this.request = req;
		this.response = res;
		this.pageContext = pc;
	}
	//业务属性
	//文章分类
	private String clsCode;
	//文章的类型
	private String artType;
	//起始记录
	private int first;
	//最大记录 
	private int max;
	//最热
	private int hot;
	//置顶 
	private int top;
	//推荐
	private int commend;
	//排序
	private String order;
	
	//标签体开始
	public boolean start(Writer writer) {
		// Create an iterator status if the status attribute was set.
		if (statusAttr != null) {
			statusState = new IteratorStatus.StatusState();
			status = new IteratorStatus(statusState);
		}
		//业务属性开始解析
		
		return true;
	}
	//标签体结束
	public boolean end(Writer writer, String body) {
		ValueStack stack = getStack();
		if (iterator != null) {
			stack.pop();
		}

		if (iterator != null && iterator.hasNext()) {
			Object currentValue = iterator.next();
			stack.push(currentValue);

			String id = getId();

			if ((id != null) && (currentValue != null)) {
				stack.getContext().put(id, currentValue);
			}

			// Update status
			if (status != null) {
				statusState.next(); // Increase counter
				statusState.setLast(!iterator.hasNext());
			}

			return true;
		} else {
			if (status != null) {
				if (oldStatus == null) {
					stack.getContext().put(statusAttr, null);
				} else {
					stack.getContext().put(statusAttr, oldStatus);
				}
			}
			super.end(writer, "");
			return false;
		}
	}

	/**
	 * if specified, an instanceof IteratorStatus will be pushed into stack upon
	 * each iteration
	 * 
	 * @s.tagattribute required="false" type="Boolean" default="false"
	 */
	public void setStatus(String status) {
		this.statusAttr = status;
	}
	
	
	//业务属性的get 和 set

	public String getClsCode() {
		return clsCode;
	}

	public void setClsCode(String clsCode) {
		this.clsCode = clsCode;
	}

	public String getArtType() {
		return artType;
	}

	public void setArtType(String artType) {
		this.artType = artType;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getHot() {
		return hot;
	}

	public void setHot(int hot) {
		this.hot = hot;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	public int getCommend() {
		return commend;
	}

	public void setCommend(int commend) {
		this.commend = commend;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}
	
}
